package com.vhall.component.controller.order;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.vhall.component.controller.order.facade.OrderAdminFacade;
import com.vhall.component.entity.order.vo.IncomeReqVo;
import com.vhall.component.entity.order.vo.OrderListReqVo;
import com.vhall.component.entity.order.vo.OrderPage;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.order.IncomeService;
import com.vhall.component.service.order.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * @author guoxin.wang
 * @date 2021/7/5 10:35
 */
@VhallComponent
public class OrderAdminController implements OrderAdminFacade {

    @Autowired
    private OrderService orderService;

    @Autowired
    private IncomeService incomeService;

    @TokenAuth("sys:order:list")
    @Override
    public OrderPage list(OrderListReqVo reqVO) {
        // 校验时间先后性
        if (StringUtils.isNotBlank(reqVO.getStartTime()) && StringUtils.isNotBlank(reqVO.getEndTime())) {
            String startTime = reqVO.getStartTime();
            String endTime = reqVO.getEndTime();
            Date start = DateUtil.parse(startTime, DatePattern.NORM_DATE_FORMAT);
            Date end = DateUtil.parse(endTime, DatePattern.NORM_DATE_FORMAT);
            if (start.after(end)) {
                throw new BusinessException(BizErrorCode.BIZ_INVALID_TIME);
            }
        }
        if (Validator.hasChinese(reqVO.getAppId())) {
            throw new BusinessException(BizErrorCode.BIZ_CHINESE_NOT_ALLOWED);
        }
        return orderService.list(reqVO);
    }

    @TokenAuth("sys:order:info")
    @Override
    public Map<String, String> income(IncomeReqVo reqVO) {
        if (Validator.hasChinese(reqVO.getAppId())) {
            throw new BusinessException(BizErrorCode.BIZ_CHINESE_NOT_ALLOWED);
        }
        return incomeService.getInfo(reqVO);
    }

}
